<%@ page contentType="text/html;charset=UTF-8" language="java" %>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core" %>


<%@include file="head.jsp"%>
<style>
    .layui-form-item .layui-form-checkbox {
        margin-top: 14px;
    }


</style>

<form class="layui-form" id="form1" style="height: 800px;">
    <div class="layui-card">
        <div class="layui-card-header"  style="color:#d02e25;font-family: '楷体'; ">
            添加权限
        </div>
        <div class="layui-card-body">
            <div class="layui-tab">
                <ul class="layui-tab-title" id="tabTitle">
                    <c:forEach var="levelOne" begin="0" end="0" items="${levelOne}">
                        <li class="layui-this">${levelOne.getName()}</li>
                    </c:forEach>

                    <c:forEach var="levelOne" begin="1" end="8" items="${levelOne}">
                        <li>${levelOne.getName()}</li>
                    </c:forEach>
                </ul>
                <div class="layui-tab-content" id="tabContent">
                    <c:forEach var="levelOne" begin="0" end="0" items="${levelOne}">
                        <div class="layui-tab-item layui-show">
                            <div class="layui-card-header">
                                页面访问权限
                            </div>
                            <label class="layui-form-label">一键加满</label>
                            <input type="checkbox" name="allChoose" lay-skin="primary" lay-filter="allChoose">
                            <div class="layui-form-item">


                                <c:forEach var="levelTwo" items="${levelTwo}">

                                    <c:if test="${levelTwo.getSuperId() == levelOne.getId()}">
                                        <input type="checkbox" name="auths" title="${levelTwo.getName()}" value="${levelTwo.getValue()}">
                                    </c:if>

                                </c:forEach>
                            </div>
                            <div class="layui-card-header">
                                请求全权限
                            </div>

                            <div class="layui-form-item">
                                <c:forEach var="levelThree" items="${levelThree}">

                                    <c:if test="${levelThree.getSuperId() == levelOne.getId()}">
                                        <input type="checkbox" name="auths" title="${levelThree.getName()}" value="${levelThree.getValue()}">
                                    </c:if>

                                </c:forEach>
                            </div>

                        </div>
                    </c:forEach>

                    <c:forEach var="levelOne" begin="1" end="8" items="${levelOne}">

                        <div class="layui-tab-item">

                            <div class="layui-card-header">
                                页面访问权限
                            </div>
                            <div class="layui-form-item">
                                <c:forEach var="levelTwo" items="${levelTwo}">

                                    <c:if test="${levelTwo.getSuperId() == levelOne.getId()}">
                                        <input type="checkbox" name="auths" title="${levelTwo.getName()}" value="${levelTwo.getValue()}">
                                    </c:if>

                                </c:forEach>
                            </div>


                            <div class="layui-card-header">
                                请求全权限
                            </div>
                            <div class="layui-form-item">
                                <c:forEach var="levelThree" items="${levelThree}">

                                    <c:if test="${levelThree.getSuperId() == levelOne.getId()}">
                                        <input type="checkbox" name="auths" title="${levelThree.getName()}" value="${levelThree.getValue()}">
                                    </c:if>

                                </c:forEach>
                            </div>

                        </div>
                    </c:forEach>


                </div>

                <div class="layui-form-item" style="text-align: center">
                    <div class="layui-input-block">
                        <button class="layui-btn" lay-submit lay-filter="save">保存</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
</form>

</body>



<script type="application/javascript">
    var form = layui.form;
    var layedit = layui.layedit;
    var layer = layui.layer;
    var auths ="";
    var authsName = "";
    form.render();

    jQuery.getUrlParam = function (name) {
        var reg = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
        var r = window.location.search.substr(1).match(reg);
        if (r != null) return unescape(r[2]); return null;
    }

    var id = $.getUrlParam("id");

        $.post("/ajax/auth/getAuthAndName",{
            id:id
        },function (res) {
            var data = res.auths;

            // console.log(data);
            var groupCheckbox = $("input[name='auths']");

            for (var i = 0; i < groupCheckbox.length; i++) {
                var val =groupCheckbox[i].value;


                if(data.indexOf(val)!=-1){
                    // console.log(val);
                    groupCheckbox[i].checked=true;
                    form.render();
                }}
        })

    form.on('checkbox(allChoose)', function(data){
        var child = $(data.elem).parents('div').find('input[type="checkbox"]');
        child.each(function(index, item){
            item.checked = data.elem.checked;
        });
        form.render('checkbox');
    });


        //监听提交
        form.on('submit(save)', function(data){



            $("input:checkbox[name='auths']:checked").each(function() { // 遍历name=auths的多选框
                auths += ',' + $(this).val();
                authsName += ',' + $(this).attr('title');
                console.log($(this).val());
            });

            $.post("/ajax/auth/addAuthsValue",{
                auths:auths,
                id:id,
                authsName:authsName
            },function (res) {
                    if(res.code==0){
                        layer.alert("成功");
                    }else {
                        layer.alert("保存失败");
                    }
            })

            return false;



        });
        form.render();

</script>




</html>